package com.ict.ms.targetLocation.service.api.impl;


import org.springframework.stereotype.Service;

import com.ict.ms.targetLocation.service.dao.IWarShipDao;
import com.ict.ms.targetLocation.facade.api.IWarShipService;
import com.ict.ms.targetLocation.facade.dto.PgParamsInt;
import com.ict.ms.targetLocation.facade.dto.ResultObj;
import com.ict.ms.targetLocation.facade.model.IrregularityShip;
import com.ict.ms.targetLocation.facade.model.WarShip;

import javax.annotation.Resource;
import java.util.List;

@Service("warShipService")
public class WarShipService implements IWarShipService {
    @Resource
    IWarShipDao iWarShipDao;

    /**
     * 获取总数
     *
     * @param num
     * @return
     */
    public int searchwarshipinfoCount(int num) {
        try {
            if (num<1){
                return iWarShipDao.searchwarshipinfoCountWithoutMMSI();
            }else{
                return iWarShipDao.searchwarshipinfoCount(num);
            }
        } catch (Exception e) {
            System.out.println("数据库操作失败：" + e);
            return -1;
        }
    }

    /**
     * 获取指定分页
     *
     * @param num
     * @param pageId
     * @param pageCount
     * @return
     */
    public ResultObj searchwarshipinfo(int num, int pageId, int pageCount) {
        PgParamsInt pgParamsInt = new PgParamsInt();
        pgParamsInt.setNum(num);
        //limit分页起始
        pgParamsInt.setStartPage((pageId - 1) * pageCount);
        //limit分页结束
        pgParamsInt.setEndPage(pageCount);
        ResultObj resultObj = new ResultObj();
        try {
            List<WarShip> warShipList;
            if (num<1){
                warShipList = iWarShipDao.searchwarshipinfoWithoutMMSI(pgParamsInt);
            }else {
                warShipList = iWarShipDao.searchwarshipinfo(pgParamsInt);
            }

            if (warShipList.size() < 1) {
                resultObj.setState(0);
            } else{
                resultObj.setState(1);
                resultObj.setObj(warShipList);
            }
            return resultObj;
        } catch (Exception e) {
            System.out.println("数据库操作失败："+e);
            resultObj.setState(-1);
            return resultObj;
        }
    }

    /**
     * 删除记录
     * @param id
     * @return
     */
    public boolean deletewarshipinfo(int id){
        try{
            iWarShipDao.deletewarshipinfo(id);
            return true;
        }catch (Exception e){
            System.out.println("数据库操作失败："+e);
            return false;
        }
    }


    public int searchlllegalshipsCount(int num){
        try {
            if (num<1){
                return iWarShipDao.searchlllegalshipsCountWithoutMMSI();
            }else {
                return iWarShipDao.searchlllegalshipsCount(num);
            }
        } catch (Exception e) {
            System.out.println("数据库操作失败：" + e);
            return -1;
        }
    }

    public ResultObj searchlllegalships(int num,int pageId,int pageCount){
        PgParamsInt pgParamsInt = new PgParamsInt();
        pgParamsInt.setNum(num);
        //limit分页起始
        pgParamsInt.setStartPage((pageId - 1) * pageCount);
        //limit分页结束
        pgParamsInt.setEndPage(pageCount);
        ResultObj resultObj = new ResultObj();
        try {
            List<IrregularityShip> irregularityShips;
            if (num<1){
                irregularityShips = iWarShipDao.searchlllegalshipsWithoutMMSI(pgParamsInt);
            }else {
                irregularityShips = iWarShipDao.searchlllegalships(pgParamsInt);
            }
            if (irregularityShips.size() < 1) {
                resultObj.setState(0);
            } else  {
                resultObj.setState(1);
                resultObj.setObj(irregularityShips);
            }
            return resultObj;
        } catch (Exception e) {
            System.out.println("数据库操作失败："+e);
            resultObj.setState(-1);
            return resultObj;
        }
    }

    public boolean deletesensitiveregion(int id){
        try{
            iWarShipDao.deletesensitiveregion(id);
            return true;
        }catch (Exception e){
            System.out.println("数据库操作失败："+e);
            return false;
        }
    }

}